package com.example.bitmap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageView img = null;
    Button but = null;
    Animation mAnimation;
    Button but2;
    ImageView mImg;
    AnimationDrawable mAd;
    int count = 0;
    int[] imgRes = {R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.p6};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bitmap bm = BitmapFactory.decodeResource(this.getResources(), imgRes[count]);
        img = (ImageView) findViewById(R.id.imageView);
        but = (Button) findViewById(R.id.but_next);
        but2=(Button) findViewById(R.id.but_2);
        img.setImageBitmap(bm);
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                if (count == 6) {
                    count = 0;
                }
                mAnimation = AnimationUtils.loadAnimation(MainActivity.this
                        , R.anim.my_all);
                img.startAnimation(mAnimation);
                BitmapFactory.Options opt = new BitmapFactory.Options();
                opt.inSampleSize = 2;
                opt.inDither = false;
                opt.inPreferredConfig = null;
                Bitmap bm2 = BitmapFactory.decodeResource(MainActivity.this.getResources(), imgRes[count], opt);
                img.setImageBitmap(bm2);
            }
        });

        but2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImg = (ImageView) findViewById(R.id.imageView1);
                mImg.setBackgroundResource(R.drawable.my_frameanimation);
                mAd = (AnimationDrawable) MainActivity.this.mImg.getBackground();
                mAd.start();

            }
        });
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MusicServer.stop(this);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MusicServer.play(this, R.raw.qingren);


    }



}